import hashlib
import json
import time

import requests


# 获取SHA256加密字符串
def get_sha256(s):
    sha256 = hashlib.sha256()
    sha256.update(s.encode('utf-8'))
    return sha256.hexdigest()


# 鉴权接口
def authSign():
    url = "https://openapi-smsp.getui.com/v1/sps/auth_sign"
    headers = {
        "Content-Type": "application/json; charset=UTF-8",
        "Accept": "application/json;charset=utf-8"
    }
    # 将个推短信服务提供的app对应的appkey和masterSecret，可自行替换
    appkey = "s2C074Mgtt5TgVKQw6c3l9"
    masterSecret = "yXWVicbExBA1DoVbZBUwN9"
    appId = "QUBPcglYnB7QZpSgFQ6ew3"
    timeStamp = int(time.time() * 1000)
    sign = get_sha256(appkey + str(timeStamp) + masterSecret)
    params = {
        "sign": sign,
        "timestamp": timeStamp,
        "appId": appId
    }
    apiResult = requests.post(url=url, data=json.dumps(params), headers=headers)
    print(apiResult.json())


# 鉴权
authSign()
